package reserve
import (
    //"time"
	"strings"
	"errors"
	"dhfshop/models"
	"github.com/astaxie/beego/orm"
)

//可预约项目
type Schema struct {
	SchemaId          int64       `orm:"column(schema_id);pk;auto" json:"schema_id" form:"schema_id"`
    PartnerId         int64         `orm:"column(partner_id)" json:"partner_id" form:"partner_id"`
	StoreId           int64         `orm:"column(store_id)" json:"store_id" form:"store_id"`
	SchemaType        *SchemaType `orm:"column(schema_type_id);rel(fk);on_delete(cascade)" json:"-"` 
    SchemaTypeId      int64       `orm:"-" json:"schema_type_id" form:"schema_type_id"` //
	Name              string      `orm:"column(name);size(128);null" json:"name" form:"name"`    //名称
	Desc              string      `orm:"column(desc);size(256);null" json:"desc" form:"desc"`    //详细描述
	Status            int         `orm:"column(status)" json:"status" form:"status"`
	Periods           []*Period   `orm:"reverse(many)" json:"periods" form:"-"`
}

func init() {
	orm.RegisterModel(new(Schema))
}

func AddSchema(m *Schema) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func AddMultiSchemas(list []*Schema) (err error) {
	size := len(list)
	o := models.GetDefaultDB()
	if size > 1 {
	    _, err = o.InsertMulti(size, list) 
	}else{
	   _, err = o.Insert(list[0])
	}
	return
}

func GetSchemaById(id int64) (v *Schema, err error) {
	o := models.GetDefaultDB()
	v = &Schema{SchemaId: id}
	if err = o.Read(v); err == nil {
		v.SchemaTypeId = v.SchemaType.SchemaTypeId
		return v, nil
	}
	return nil, err
}
//查询所有餐桌
func GetAllSchemas(storeIds []int64,query map[string]string,fields []string,order_by map[string]string,offset int,limit int)([]*Schema, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(Schema))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	if len(storeIds) > 0 {
        qs = qs.Filter("store_id__in", storeIds)
    }
	cnt, err := qs.Count()
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	var lists []*Schema
	qs = qs.OrderBy(sortFields...)
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		for _, schema := range lists {
			schema.SchemaTypeId = schema.SchemaType.SchemaTypeId
	    }
		return lists, cnt, nil
	}
	return nil, 0, err
}
func UpdateSchemaById(m *Schema, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := Schema{SchemaId: m.SchemaId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}
func DeleteSchema(id int64) (err error) {
	o := models.GetDefaultDB()
	v := Schema{SchemaId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}
